﻿//-----------------------------------------------------------------------
// <copyright file="OpenCloseGroupAction.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Action.Group
{
    using System;
    using System.Collections.Generic;
    using TileTool.Log;
    using TileTool.Scene.Entity;
    using TileTool.Scene.Entity.Group;

    /// <summary>
    /// Action to toggle the open and closed state of groups.
    /// </summary>
    public class OpenCloseGroupAction : SceneAction
    {
        #region Constants - Private

        /// <summary>
        /// Log message group.
        /// </summary>
        private const string LogGroup = "OpenCloseGroupAction";

        #endregion

        #region Fields - Private

        /// <summary>
        /// Groups that we are going to open / close.
        /// </summary>
        private IEntityCollection entities;

        /// <summary>
        /// Dictionary which maps from a group to its start state.
        /// </summary>
        private IDictionary<IEntity, bool> startStateDict;

        /// <summary>
        /// Previous selection of the scene.
        /// </summary>
        private IEntityCollection previousSelection;

        /// <summary>
        /// New selection for the scene.
        /// </summary>
        private IEntityCollection newSelection;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the action.
        /// </summary>
        /// <param name="scene">Scene we are creating a group in.</param>
        /// <param name="ents">Entities to create a group with.</param>
        public OpenCloseGroupAction(ISceneManager scene, IEntityCollection ents)
            : base(scene)
        {
            if (scene == null)
            {
                throw new ArgumentNullException("scene", "Scene must be non-null.");
            }

            if (ents == null)
            {
                throw new ArgumentNullException("ents", "Entity collection must be non-null..");
            }

            if (ents.Count == 0)
            {
                throw new ArgumentException("Entity collection count must be non-zero.");
            }

            this.Valid = false;

            this.entities = new EntityCollection();
            this.startStateDict = new Dictionary<IEntity, bool>();
            this.newSelection = new EntityCollection();

            foreach (IEntity entity in ents)
            {
                GroupEntity group = entity as GroupEntity;
                if (group == null)
                {
                    continue;
                }

                this.entities.Add(group);
                this.startStateDict.Add(group, group.Open);

                if (group.Selected && !group.Open)
                {
                    this.newSelection.Add(group.Children);
                }
            }

            this.Valid = this.entities.Count != 0;

            this.previousSelection = this.Scene.Selection.Clone();
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Do the action.
        /// </summary>
        public override void Do()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Do() - Toggling open / close state for {0} groups.", this.entities.Count);

            foreach (IEntity entity in this.entities)
            {
                GroupEntity group = entity as GroupEntity;

                group.Open = !this.startStateDict[group];
            }

            if (this.newSelection.Count != 0)
            {
                Scene.SetSelection(this.newSelection);
            }
        }

        /// <summary>
        /// Undo the action.
        /// </summary>
        public override void Undo()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Undo() - Toggling open / close state for {0} groups.", this.entities.Count);

            foreach (IEntity entity in this.entities)
            {
                GroupEntity group = entity as GroupEntity;

                group.Open = this.startStateDict[group];
            }

            if (this.newSelection.Count != 0)
            {
                this.Scene.SetSelection(this.previousSelection);
            }
        }

        #endregion
    }
}
